package com.xjtu.duansheli.controller;

import com.xjtu.duansheli.entity.OrderInfo;
import com.xjtu.duansheli.entity.UserInfo;
import com.xjtu.duansheli.json.AjaxResponse;
import com.xjtu.duansheli.service.OrderInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 漩涡鸣人
 * @since 2020-10-24
 */
@Slf4j
@RestController
@RequestMapping("/project/order-info")
@Api(value = "交易信息管理模块", description = "这是交易信息增删改查的模块")
public class OrderInfoController {

    @Autowired
    private OrderInfoService orderInfoService;

    @ApiOperation(value = "获取全部交易(按成交时间排序)", notes = "获取所有交易的信息(按成交时间排序)")
    @RequestMapping(value = "/getAllByendTime", method = RequestMethod.GET)
    public AjaxResponse getAllByendTime() {
        return AjaxResponse.success(orderInfoService.getOrderByendTime());
    }

    @ApiOperation(value = "获取全部交易", notes = "获取所有交易的信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", required = false, defaultValue = "1", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "size", value = "每页数量", required = false, defaultValue = "10", paramType = "query", dataType = "int")
    })
    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    public AjaxResponse getAll(@RequestParam(value = "current") Integer current,
                               @RequestParam(value = "size") Integer size) {
        return AjaxResponse.success(orderInfoService.getAllOrder(current, size));
    }

    @ApiOperation(value = "根据id查找交易", notes = "获取根据id查找到的交易")
    @ApiImplicitParam(name = "id", value = "交易id", required = true, paramType = "query", dataType = "Long")
    @RequestMapping(value = "/getOrderById", method = RequestMethod.GET)
    public AjaxResponse getOrderById(@RequestParam(name = "id") Long id) {
        return AjaxResponse.success(orderInfoService.getOrderByid(id));
    }

    @ApiOperation(value = "根据交易id查找对应的交易", notes = "查找某个交易的全部交易记录")
    @ApiImplicitParam(name = "pid", value = "交易id", required = true, paramType = "query", dataType = "Long")
    @RequestMapping(value = "/getOrderBypId", method = RequestMethod.GET)
    public AjaxResponse getOrderBypId(@RequestParam(name = "pid") Long pid) {
        return AjaxResponse.success(orderInfoService.getOrderBypid(pid));
    }

    @ApiOperation(value = "根据卖家id查找他进行全部交易", notes = "查找某个卖家进行的全部交易")
    @ApiImplicitParam(name = "aid", value = "卖家id", required = true, paramType = "query", dataType = "Long")
    @RequestMapping(value = "/getOrderBysId", method = RequestMethod.GET)
    public AjaxResponse getOrderBysId(@RequestParam(name = "sid") Long sid) {
        return AjaxResponse.success(orderInfoService.getOrderBysaleId(sid));
    }

    @ApiOperation(value = "根据买家id查找他进行全部交易", notes = "查找某个买家进行的全部交易")
    @ApiImplicitParam(name = "bid", value = "买家id", required = true, paramType = "query", dataType = "Long")
    @RequestMapping(value = "/getOrderBybId", method = RequestMethod.GET)
    public AjaxResponse getOrderBypaId(@RequestParam(name = "bid") Long bid) {
        return AjaxResponse.success(orderInfoService.getOrderBybuyId(bid));
    }

    @ApiOperation(value = "添加交易信息", notes = "获取交易信息并且添加")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "result", value = "交易结果(1成功；2失败；3延期)", required = true, paramType = "form", dataType = "int"),
            @ApiImplicitParam(name = "endTime", value = "完成时间", required = true, defaultValue = "Mon Mar 23 11:04:12 CST 2020", paramType = "form", dataType = "Date"),
            @ApiImplicitParam(name = "productId", value = "所属商品id", required = true, paramType = "form", dataType = "Long"),
            @ApiImplicitParam(name = "salerId", value = "卖家id", required = true, paramType = "form", dataType = "Long")
    })
    @RequestMapping(value = "/addOrder", method = RequestMethod.POST)
    public AjaxResponse addOrder(@RequestParam(value = "result") Integer result,
                                 @RequestParam(value = "endTime") Date endTime,
                                 @RequestParam(value = "productId") Long pid,
                                 @RequestParam(value = "salerId") Long sid,
                                 HttpServletRequest request) {
        UserInfo user = (UserInfo) request.getSession().getAttribute("loginUser");
        OrderInfo orderInfo = new OrderInfo()
                .setResult(result)
                .setProductId(pid)
                .setSalerId(sid)
                .setEndTime(endTime)
                .setBuyerId(user.getUid());
        return AjaxResponse.success("数据库受影响行数->" + orderInfoService.addOrder(orderInfo));
    }

    @ApiOperation(value = "修改交易信息", notes = "录入新的交易信息并且修改")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "oid", value = "交易id", required = true, paramType = "form", dataType = "int"),
            @ApiImplicitParam(name = "result", value = "交易结果(1成功；2失败；3延期)", required = false, defaultValue = "1", paramType = "form", dataType = "int"),
            @ApiImplicitParam(name = "endTime", value = "完成时间", required = false, paramType = "form", dataType = "Date")
    })
    @RequestMapping(value = "/updateOrder", method = RequestMethod.PUT)
    public AjaxResponse updateOrder(@RequestParam(value = "oid") Long oid,
                                    @RequestParam(value = "result") Integer result,
                                    @RequestParam(value = "endTime") Date endTime) {
        OrderInfo orderInfo = new OrderInfo()
                .setOid(oid)
                .setEndTime(endTime)
                .setResult(result);
        return AjaxResponse.success("数据库受影响行数->" + orderInfoService.updateOrder(orderInfo));
    }

    @ApiOperation(value = "删除交易信息", notes = "从数据库中删除某个交易")
    @ApiImplicitParam(name = "oid", value = "删除交易id", required = true, paramType = "query", dataType = "Long")
    @RequestMapping(value = "/deleteOrder", method = RequestMethod.DELETE)
    public AjaxResponse deleteOrder(@RequestParam(value = "oid") Long oid) {
        return AjaxResponse.success("数据库受影响行数->" + orderInfoService.deleteOrder(oid));
    }

    @ApiOperation(value = "交易总数量", notes = "获取数据库中交易总数量")
    @RequestMapping(value = "/getTotal", method = RequestMethod.GET)
    public AjaxResponse getTotal() {
        return AjaxResponse.success(orderInfoService.getTotal());
    }
}

